package com.pray;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * <p>
 * MybatisCodeGenerator
 * <p>
 *
 * @author 春江花朝秋月夜
 * @since 2023/9/25 13:19
 */
public class MybatisCodeGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
    public static ArrayList<FileOutConfig> template(String[] args,PackageConfig config){
        ArrayList<FileOutConfig> list=new ArrayList<>();
        String[]trans={"Controller","Mapper","Mapper","Service","Entity","ServiceImpl"};
        String []type={".java",".xml",".java",".java",".java",".java"};
        for (int i=0;i<args.length;i++) {
            int finalI = i;
            FileOutConfig outConfig = new FileOutConfig(args[finalI]) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                    if (!trans[finalI].equals("Entity")){
                        String s = "D:\\JavaWork\\pary_analysis\\backend"+ "/src/main/java/compile/"+trans[finalI].toLowerCase() + config.getModuleName()
                                + "/" + tableInfo.getEntityName() +trans[finalI]+ type[finalI];
                        return s;
                    }else if (trans[finalI].equals("ServiceImpl")){
                        return "D:\\JavaWork\\pary_analysis\\backend"+ "/src/main/java/compile/service/impl/" + config.getModuleName()
                                + "/" + tableInfo.getEntityName()+ type[finalI];
                    }
                    return "D:\\JavaWork\\pary_analysis\\backend"+ "/src/main/java/compile/"+trans[finalI].toLowerCase() + config.getModuleName()
                            + "/" + tableInfo.getEntityName()+ type[finalI];
                }
            };
            list.add(outConfig);
        }
        list.forEach(System.out::println);
        return list;
    }
    public static void main(String[] args) {
        String []path={"/templates/controller.java.vm","/templates/mapper.xml.vm","/templates/mapper.java.vm",
                "/templates/service.java.vm","/templates/entity.java.vm","/templates/serviceImpl.java.vm"};
        //根据自己的模板生成代码：
        AutoGenerator generator=new AutoGenerator();//构造代码生成器
        GlobalConfig conf=new GlobalConfig();
        conf.setOutputDir( "/src/main/java");
        conf.setAuthor("春江花朝秋月夜");
        conf.setOpen(false);
        conf.setBaseResultMap(true);
        conf.setBaseColumnList(true);
        conf.setServiceName("%sService");
        generator.setGlobalConfig(conf);

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/bootdemo?useSSL=false&serverTimezone=Asia/Shanghai");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        generator.setDataSource(dsc);
        PackageConfig packageConf = new PackageConfig();

        packageConf.setParent("com.pray.compile")
                .setEntity("entity")
                .setMapper("mapper")
                .setService("service")
                .setController("controller")
                .setServiceImpl("service.impl");
        generator.setPackageInfo(packageConf);

        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        cfg.setFileOutConfigList(template(path,packageConf));
        generator.setCfg(cfg);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);
        generator.setTemplate(templateConfig);

        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);

        generator.setStrategy(strategy);
        generator.setTemplateEngine(new VelocityTemplateEngine());
        generator.execute();
    }
}
